//
//  FileManager.swift
//  TheWatermark
//
//  Created by 沙睿 on 2021/6/6.
//

import Foundation
import SwiftUI

var KNameDict = [String: Int]()

func nameDict(key: String) -> String {
    let index = KNameDict[key] ?? 0
    KNameDict[key] = index + 1
    return index == 0 ? "" : String(index)
}
struct FileManager {
    static func saveFile(_ data: NSData, path: String, name: String, allowedFileTypes: [String] = ["jpg","png"]) {
        //设定好文件路径后进行存储就ok了
        if path.count == 0 {
            return
        }
        let path = "\(path)/\(name)"
        let succeed = data.write(toFile: path, atomically: false)
        print("存储:" + "\(succeed ? "成功": "失败")")
        

        
        return  
        
        
        let panel = NSSavePanel()
        panel.title = "保存文件"
        panel.message = "请选择文件保存地址"
        panel.directoryURL = URL(string: "\(NSHomeDirectory())/Desktop")
        panel.nameFieldStringValue = name
        panel.allowsOtherFileTypes = true
        panel.allowedFileTypes = allowedFileTypes
        panel.isExtensionHidden = false
        panel.canCreateDirectories = true
        
        panel.begin { (response) in
            if response == .OK {
                if let path = panel.url?.path {
                    data.write(toFile: path, atomically: true)
                }
            }
        }
    }
    
    typealias FileDataBlock = (_ imageData: ImageDataSource)->(Void)
    
    static func openFile(dataBlock: FileDataBlock?) -> Void{
        
        let openPanel = NSOpenPanel()
        // 是否可以选择文件夹
        openPanel.canChooseDirectories = true
        // 是否可以选择文件
        openPanel.canChooseFiles = true
        // 是否可以多选
        openPanel.allowsMultipleSelection = true
        // 设置可选文件后缀名
        openPanel.allowedFileTypes=["png","jepg","gif","jpg"]
        
        // 滑出式窗口
        let okPressed = openPanel.runModal() == .OK
        if okPressed {
            
            for url in openPanel.urls {
                let path = url.path
                let image = NSImage(contentsOfFile: path)!
                let name = path.components(separatedBy: "/").last ?? ""
                guard let dataBlock = dataBlock else {
                    return
                }
                dataBlock(ImageDataSource(name:name,image: image, id: path.hash, path: path))
            }
        }
    }
       
    typealias SelectedFileDataBlock = (_ path :String)->(Void)

    static func selectedFile(pathBlock: SelectedFileDataBlock?) -> Void{
        
        let openPanel = NSOpenPanel()
        // 是否可以选择文件夹
        openPanel.canChooseDirectories = true
        // 是否可以选择文件
        openPanel.canChooseFiles = false
        // 是否可以多选
        openPanel.allowsMultipleSelection = false
        // 滑出式窗口
        let okPressed = openPanel.runModal() == .OK
        if okPressed {
            
            for url in openPanel.urls {
                let path = url.path
                guard let pathBlock = pathBlock else {
                    return
                }
                pathBlock(path)
            }
        }
    }
}
